package com.seelyn.ddvideo.spider;

import com.seelyn.ddvideo.event.SystemEventPublisher;
import com.seelyn.ddvideo.model.Video;
import com.seelyn.ddvideo.service.VideoService;
import com.seelyn.ddvideo.utils.DateUtils;
import org.jsoup.nodes.Element;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.util.Date;
import java.util.List;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/21
 */
public class KuyunVideoPageProcessor implements PageProcessor {

    private String uploadDir;
    private String uploadSite;
    private VideoService videoService;
    private SystemEventPublisher eventPublisher;
    private String xpathString = "/html/body/div[@class*='xing_vb']/ul/li/span[@class*='xing_vb4']/a";
    private String typeXpathString = "/html/body/div[@class*='xing_vb']/ul/li/span[@class*='xing_vb5']";
    private String onlineXpathString = "/html/body/div[@class*='xing_vb']/ul/li/span[@class*='xing_vb6']";

    private Site site = Site.me().setDomain("http://ziyuanpian.com")
            .setCharset("UTF-8")
            .setSleepTime(500)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

    public KuyunVideoPageProcessor(VideoService videoService, SystemEventPublisher eventPublisher, String uploadDir, String uploadSite) {
        this.videoService = videoService;
        this.eventPublisher = eventPublisher;
        this.uploadDir = uploadDir;
        this.uploadSite = uploadSite;
    }

    @Override
    public void process(Page page) {

        Video video = null;
        Html body = page.getHtml();
        Selectable spanItems = body.xpath(xpathString);
        if (spanItems == null || spanItems.all().isEmpty()) {
            return;
        }
        List<String> items = spanItems.all();

        Selectable typeSpanItems = body.xpath(typeXpathString);
        if (typeSpanItems == null || typeSpanItems.all().isEmpty()) {
            return;
        }
        List<String> typeItems = typeSpanItems.all();

        Selectable onlineSpanItems = body.xpath(onlineXpathString);
        if (onlineSpanItems == null || onlineSpanItems.all().isEmpty()) {
            return;
        }
        List<String> onlineItems = onlineSpanItems.all();

        Html itemHtml;
        int count = items.size() - 1;
        for (int index = count; index >= 0; index--) {

            String item = items.get(index);
            itemHtml = Html.create(item);

            Element aElement = itemHtml.getDocument().body().child(0);
            String link = aElement.attr("href");
            String name = aElement.ownText();

            if (StringUtils.isEmpty(name) || StringUtils.isEmpty(link)) {
                continue;
            }

            video = videoService.findVideoByTitle(name);
            if (video == null) {
                video = new Video();
            }

            video.setMovieType(MovieTypeEnum.movie.ordinal());
            video.setDelFlag(false);
            video.setTitle(name);
            video.setScore(3.0F);
            video.setCreateTime(new Date().getTime());
            video.setCreateBy("1");

            if (typeItems.size() > index) {
                itemHtml = Html.create(typeItems.get(index));
                Element typeElement = itemHtml.getDocument().body().child(0);
                if (typeElement != null) {
                    String typeText = typeElement.ownText();
                    MovieSubTypeEnum typeEnum = MovieSubTypeEnum.getEnum(typeText);
                    video.setSubType(typeEnum.ordinal());
                }
            }

            if (onlineItems.size() > index) {
                itemHtml = Html.create(onlineItems.get(index));
                Element onlineElement = itemHtml.getDocument().body().child(0);
                if (onlineElement != null) {
                    String onlineText = onlineElement.ownText();
                    Date onLineDate = DateUtils.parseSmall(onlineText);
                    video.setOnlineTime(onLineDate.getTime());
                }
            }

            Spider.create(new KuyunVideoInfoMovieProcessor(eventPublisher, videoService, video, uploadDir, uploadSite))
                    .addUrl(site.getDomain() + link)
                    .run();

        }

    }

    @Override
    public Site getSite() {
        return site;
    }


}
